package com.fly.filter;

import com.sun.deploy.net.HttpResponse;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebFilter(filterName = "CrossDemain",urlPatterns = "/*")
public class CrossDomainFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ((HttpServletResponse)servletResponse).addHeader("Access-Control-Allow-Headers","Accept, Origin, XRequestedWith, Content-Type, LastModified");//允许请求头方式访问
        ((HttpServletResponse)servletResponse).addHeader("Access-Control-Allow-Origin","*");//允许所有来源访问
        ((HttpServletResponse)servletResponse).addHeader("Access-Control-Allow-Method","POST,GET");//允许访问方式
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
